package com.xiaoge.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoge.response.Result;
import com.xiaoge.response.ResultCode;
import com.xiaoge.system.entity.*;
import com.xiaoge.system.mapper.MenuMapper;
import com.xiaoge.system.mapper.RoleMapper;
import com.xiaoge.system.mapper.RoleMenuMapper;
import com.xiaoge.system.mapper.UserRoleMapper;
import com.xiaoge.system.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author xiaoge
 * @since 2021-02-02
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 通过角色id查询角色拥有的菜单权限id
     *
     * @param id
     * @return
     */
    @Override
    public List<Long> findMenuIdsByRoleId(Long id) {
        Role role = roleMapper.selectById(id);
        if (role == null) {
            throw new Error("该角色已不存在");
        }
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", id);
        List<Long> ids = new ArrayList<>();
        List<RoleMenu> roleMenus = roleMenuMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(roleMenus)) {
            for (RoleMenu roleMenu : roleMenus) {
                ids.add(roleMenu.getMenuId());
            }
        }
        return ids;
    }

    @Override
    public void authority(Long id, Long[] mids) {
        //删除之前角色的菜单
        Role role = roleMapper.selectById(id);
        if (role == null) {
            throw new Error("角色不存在");
        }
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", id);
        //删除之前分配的
        roleMenuMapper.delete(queryWrapper);
        //增加现在分配的
        if (mids.length > 0) {
            for (Long mid : mids) {
                Menu menu = menuMapper.selectById(mid);
                if (menu == null) {
                    throw new Error("menuId=" + mid + ",该菜单不存在");
                } else {
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setRoleId(id);
                    roleMenu.setMenuId(mid);
                    roleMenuMapper.insert(roleMenu);
                }
            }
        }
        //清除所有菜单redis缓存
        Set<String> keys=redisTemplate.keys("menus_"+"*");
        redisTemplate.delete(keys);
    }

    /**
     * 更新角色
     * @param role
     */
    @Override
    public void updateStatus(Role role) {
        roleMapper.updateById(role);
        //清除所有菜单redis缓存
        Set<String> keys=redisTemplate.keys("menus_"+"*");
        redisTemplate.delete(keys);
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @Override
    public Result delRole(Long id) {
        QueryWrapper<UserRole>queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("role_id",id);
        Integer count=userRoleMapper.selectCount(queryWrapper);
        if (count!=0){
            return Result.error(ResultCode.ROLE_HAVE_USER);
        }
        roleMapper.deleteById(id);
        return Result.ok();
    }
}
